package ru.ifmo.ipimenov.ewf_svn.standalone.model;

import org.apache.commons.logging.LogFactory;
import org.apache.commons.logging.Log;
import org.tmatesoft.svn.core.SVNURL;
import org.tmatesoft.svn.core.SVNException;
import ru.ifmo.ipimenov.ewf_svn.standalone.model.list.Item;
import ru.ifmo.ipimenov.ewf_svn.standalone.model.db.User;
import ru.ifmo.ipimenov.ewf_svn.standalone.model.db.Document;
import ru.ifmo.ipimenov.ewf_svn.standalone.dao.DocumentDao;
import ru.ifmo.ipimenov.ewf_svn.standalone.dao.impl.DocumentDaoHibernate;

import java.util.List;
import java.util.ArrayList;

/**
 * @author: Ilya Pimenov
 * Scartel Starlab company
 */
public class ItemProvider {
    /**
     * Logger for all purposes
     */
    private static Log log = LogFactory.getLog(ItemProvider.class);

    private static ItemProvider instance = new ItemProvider();

    private DocumentDao dao = new DocumentDaoHibernate();

    public static ItemProvider getInstance(){
        return instance;
    }

    public List<Item> loadItems(User u){
        List<Document> ld = dao.load(u);
        List<Item> li = new ArrayList<Item>();
        for (Document d : ld){
            try {
                li.add(new Item(d.getName(), SVNURL.parseURIEncoded(d.getUrl()), null, d));
            } catch (SVNException e) {
                log.error("Failed to parse document location to SVN url");
            }
        }
        return li;
    }

}
